package com.cskaoyan.wordmemorize.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cskaoyan.wordmemorize.converter.VocChapterConverter;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.*;
import com.cskaoyan.wordmemorize.dao.mapper.*;
import com.cskaoyan.wordmemorize.dto.admin.VocExcelDTO;
import com.cskaoyan.wordmemorize.request.ExampleSentenceCommand;
import com.cskaoyan.wordmemorize.request.VocMeaningCommand;
import com.cskaoyan.wordmemorize.service.impl.VocServiceImpl;
import groovy.util.logging.Slf4j;
import org.bouncycastle.jcajce.provider.symmetric.RC2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class VocFileExcelDataListener implements ReadListener<VocExcelDTO> {

    VocConverter vocConverter;

    VocChapBookMapper vocChapBookMapper;
    VocChapterMapper vocChapterMapper;
    VocBookMapper vocBookMapper;
    VocMapper vocMapper;
    VocMeaningMapper vocMeaningMapper;
    ExampleSentenceMapper exampleSentenceMapper;


    public VocFileExcelDataListener(VocConverter vocConverter, VocChapBookMapper vocChapBookMapper, VocChapterMapper vocChapterMapper, VocBookMapper vocBookMapper, VocMapper vocMapper, VocMeaningMapper vocMeaningMapper, ExampleSentenceMapper exampleSentenceMapper) {
        this.vocConverter = vocConverter;
        this.vocChapBookMapper = vocChapBookMapper;
        this.vocChapterMapper = vocChapterMapper;
        this.vocBookMapper = vocBookMapper;
        this.vocMapper = vocMapper;
        this.vocMeaningMapper = vocMeaningMapper;
        this.exampleSentenceMapper = exampleSentenceMapper;
    }


    @Override
    public void invoke(VocExcelDTO vocExcelDTO, AnalysisContext analysisContext) {
        VocExcelParsedDO vocExcelParsedDO = new VocExcelParsedDO();
        VocDO vocDO = vocConverter.vocExcelDTO2VocDO(vocExcelDTO);
        vocMapper.insert(vocDO);
        // 处理释义
        String interpretation = vocExcelDTO.getInterpretations();
        String[] interpretationArray = interpretation.split("\n");
        vocDO.setInterpretations(new ArrayList<VocMeaningDO>());
        for (String interpretationStr : interpretationArray) {
            String[] interpretationStrArray = interpretationStr.split("\\.");
            VocMeaningDO vocMeaningDO = new VocMeaningDO();
            vocMeaningDO.setType(interpretationStrArray[0] + ".");
            vocMeaningDO.setInterContent(interpretationStrArray[1]);
            vocMeaningDO.setVocId(vocDO.getId());
            vocDO.getInterpretations().add(vocMeaningDO);
        }
        // 处理例句
        String exampleSentences = vocExcelDTO.getExampleSentences();
        String[] exampleSentencesArray = exampleSentences.split("\n");
        ArrayList<ExampleSentenceDO> exampleSentenceDOS = new ArrayList<>();
        for (int i = 0; i < exampleSentencesArray.length; i++) {
            if (i % 2 != 0) {
                continue;
            }
            ExampleSentenceDO exampleSentenceDO = new ExampleSentenceDO();
            exampleSentenceDO.setVocId(vocDO.getId());
            exampleSentenceDO.setContent(exampleSentencesArray[i]);
            exampleSentenceDO.setMeaning(exampleSentencesArray[i + 1]);
            exampleSentenceDOS.add(exampleSentenceDO);
        }
        vocDO.setExamples(exampleSentenceDOS);


        // 处理书籍和章节
        // 格式为：书籍#章节 或者 书籍
        String bookInfos = vocExcelDTO.getBookInfos();
        String[] bookInfoArray = bookInfos.split("#");

        vocExcelParsedDO.setVocChapBooks(new ArrayList<VocChapBookDO>());
        if (bookInfoArray.length == 1) {
            VocChapBookDO vocChapBookDO = new VocChapBookDO();
            vocChapBookDO.setVocId(vocDO.getId());
            vocChapBookDO.setVocName(vocDO.getContent());
            // 书籍名称
            LambdaQueryWrapper<VocBookDO> vocBookWrapper = new LambdaQueryWrapper<>();
            vocBookWrapper.eq(VocBookDO::getName, bookInfoArray[0]);
            VocBookDO vocBookDO = vocBookMapper.selectOne(vocBookWrapper);

            vocChapBookDO.setBookId(vocBookDO.getId());
            vocChapBookDO.setBookName(bookInfoArray[0]);

            vocExcelParsedDO.getVocChapBooks().add(vocChapBookDO);
        } else if (bookInfoArray.length == 2) {
            VocChapBookDO vocChapBookDO = new VocChapBookDO();
            vocChapBookDO.setVocId(vocDO.getId());
            vocChapBookDO.setVocName(vocDO.getContent());
            // 书籍名称
            LambdaQueryWrapper<VocBookDO> vocBookWrapper = new LambdaQueryWrapper<>();
            vocBookWrapper.eq(VocBookDO::getName, bookInfoArray[0]);
            VocBookDO vocBookDO = vocBookMapper.selectOne(vocBookWrapper);

            vocChapBookDO.setBookId(vocBookDO.getId());
            vocChapBookDO.setBookName(bookInfoArray[0]);

            // 章节名称
            LambdaQueryWrapper<VocChapterDO> vocChapterWrapper = new LambdaQueryWrapper<>();
            vocChapterWrapper.eq(VocChapterDO::getChapterName, bookInfoArray[1]);
            VocChapterDO vocChapterDO = vocChapterMapper.selectOne(vocChapterWrapper);
            vocChapBookDO.setChapterId(vocChapterDO.getId());
            vocChapBookDO.setChapterName(bookInfoArray[1]);

            vocExcelParsedDO.getVocChapBooks().add(vocChapBookDO);

        }

        vocExcelParsedDO.setVoc(vocDO);
        saveData(vocExcelParsedDO);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    private void saveData(VocExcelParsedDO data) {
        VocDO vocDO = data.getVoc();

        List<VocChapBookDO> relations = data.getVocChapBooks();
        if (relations != null) {
            for (VocChapBookDO vocChapBookDO : relations) {
                // 设置单词id
                vocChapBookDO.setVocId(vocDO.getId());
                vocChapBookMapper.insert(vocChapBookDO);

                // 修改书籍和章节关联的单词数量
                if (vocChapBookDO.getChapterId() != null) {
                    // 关联到了一个章节
                    LambdaUpdateWrapper<VocChapterDO> vocChapterWrapper = new LambdaUpdateWrapper<>();
                    vocChapterWrapper.eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                            // set voc_count = voc_count + 1
                            .setSql("voc_count = voc_count + 1");

                    vocChapterMapper.update(null, vocChapterWrapper);
                }


                // 更新书籍关联的单词数量
                // 关联到了一个章节
                LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
                vocBookWrapper.eq(VocBookDO::getId, vocChapBookDO.getBookId())
                        // set voc_count = voc_count + 1
                        .setSql("voc_count = voc_count + 1");
                vocBookMapper.update(null, vocBookWrapper);

            }
        }


        // 保存释义
        List<VocMeaningDO> interpretations = data.getVoc().getInterpretations();
        for (VocMeaningDO vocMeaningDO : interpretations) {
            vocMeaningDO.setVocId(vocDO.getId());
            vocMeaningMapper.insert(vocMeaningDO);
        }


        // 保存例句
        List<ExampleSentenceDO> examples = data.getVoc().getExamples();
        for (ExampleSentenceDO exampleSentenceDO : examples) {
            exampleSentenceDO.setVocId(vocDO.getId());
            exampleSentenceMapper.insert(exampleSentenceDO);
        }

    }
}
